package com.aplink.generic.database;

import com.aplink.generic.constant.SqlDataType;

public class SqlMethods {

    private StringBuilder mWhereClause;
    private static final String EQUAL = " = ";
    private static final String LIKE = " LIKE ";
    private static final String GREATER_THAN = " >= ";
    private static final String LESS_THAN = " <= ";
    private static final String AND = " AND ";
    private static final String OR = " OR ";

    private SqlMethods() {
        mWhereClause = new StringBuilder();
    }

    public static SqlMethods getInstance() {
        return new SqlMethods();
    }

    public String and( String... clause ) {
        StringBuilder stringBuilder = new StringBuilder();
        for ( int i = 0 ; i < clause.length ; i++ ) {
            stringBuilder.append( clause[i] );
            if ( i < clause.length - 2 ) {
                stringBuilder.append( AND );
                stringBuilder.append( clause[i + 1] );
            }
        }
        return stringBuilder.toString();
    }

    public SqlMethods equal( SqlDataType sqlDataType, String column, String value ) {
        if ( sqlDataType.equals( SqlDataType.TEXT ) ) {
            mWhereClause.append( String.format( "UPPER(%s)=UPPER('%s')", column, value ) );
        } else if ( sqlDataType.equals( SqlDataType.INTEGER ) ||
                sqlDataType.equals( SqlDataType.DOUBLE ) ||
                sqlDataType.equals( SqlDataType.LONG ) || sqlDataType.equals( SqlDataType.REAL ) ) {
            mWhereClause.append( String.format( "%s=%s", column, value ) );
        }
        return this;
    }

    public SqlMethods and( String clause ) {
        mWhereClause.append( AND ).append( clause );
        return this;
    }

    public SqlMethods or( String clause ) {
        mWhereClause.append( AND ).append( clause );
        return this;
    }

    public SqlMethods like( String column, String value ) {
        mWhereClause.append( String.format( "%s LIKE 's'", column, value ) );
        return this;
    }

    @Override
    public String toString() {
        return mWhereClause.toString();
    }
}
